package com.app.catchgpscoord;

import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity{

	    LocationManager mLocationManager;
		
		TextView txtv_latitude;
		EditText txtv_length;
		EditText txtv_description;
		Spinner spn_type_location;
		Spinner spn_building;
		TextView txtv_number;
		Button btn_catch;
		Button btn_save;
		HandlerSqlite hq_dataBase;
		GPS_handler gps_hnd;
		int count_rows;
		
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	        
	        txtv_latitude = (EditText) findViewById(R.id.txt_latitude);
	        txtv_length = (EditText) findViewById(R.id.txt_length);
			btn_catch = (Button) findViewById(R.id.btn_catch);
			btn_save = (Button) findViewById(R.id.btn_save_on_db);
			spn_type_location = (Spinner) findViewById(R.id.spn_type_location);
			spn_building = (Spinner) findViewById(R.id.spn_building);
			txtv_number = (TextView) findViewById(R.id.txt_number);
			
			spn_type_location.setEnabled(false);
			spn_building.setEnabled(false);
			txtv_number.setEnabled(false);
			
			//Asignacion de valores a Spinners
			ArrayAdapter<?> adapter_type_location = ArrayAdapter.createFromResource(this, R.array.type_location, android.R.layout.simple_spinner_item);
			adapter_type_location.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	        spn_type_location.setAdapter(adapter_type_location);
	        ArrayAdapter<?> adapter_building = ArrayAdapter.createFromResource(this, R.array.buildings, android.R.layout.simple_spinner_item);
	        adapter_building.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spn_building.setAdapter(adapter_building);
			
			hq_dataBase = new HandlerSqlite(this);
			mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
			gps_hnd = new GPS_handler(txtv_latitude, txtv_length, this, mLocationManager); 
			
			btn_catch.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View view) {
	            	txtv_latitude.setText("");
	            	txtv_length.setText("");
	            	txtv_number.setText("");
	            	spn_type_location.setEnabled(false);
        			spn_building.setEnabled(false);
        			txtv_number.setEnabled(false);
        			btn_save.setEnabled(false);
	            	gps_hnd.writeSignalGPS();
	            	
	                	spn_type_location.setEnabled(true);
	        			spn_building.setEnabled(true);
	        			txtv_number.setEnabled(true);
	        			btn_save.setEnabled(true);
	            }      
	        });
			
			btn_save.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View view) {
	            	count_rows = 0;
	            	if(	txtv_latitude.getText().toString().length()>0 && 
	            		txtv_length.getText().toString().length()>0 && 
	            		spn_type_location.getSelectedItem().toString().length()>0 &&
	            		spn_building.getSelectedItem().toString().length()>0 &&
	            		txtv_number.getText().toString().length()>0)
	            	{
	            		count_rows = (int)hq_dataBase.insertRowOnTable_halls_location(	spn_type_location.getSelectedItem().toString(),
								spn_building.getSelectedItem().toString(), 
								txtv_number.getText().toString(),
								txtv_latitude.getText().toString(), 
								txtv_length.getText().toString());
	            	}
	            		
	    			validate_insert();	    			
	            }      
	        });
			
	        
	    }
	    
	    private void validate_insert()
	    {
	        AlertDialog.Builder alert = new AlertDialog.Builder(this);
	        alert.setTitle(getResources().getString(R.string.str_warning));
	        alert.setPositiveButton("OK", new OnClickListener() {
		        public void onClick(DialogInterface dialog, int which) {
		            dialog.cancel();
		        }
		    });
			switch (count_rows)
			{
				case -1:
					alert.setMessage(getResources().getString(R.string.str_duplicate_row));
					break;
				case 0:
					alert.setMessage(getResources().getString(R.string.str_fail_insert));
					break;
				default:
					alert.setMessage(getResources().getString(R.string.str_successful_insert));
					break;
			}
			alert.create();
			alert.show();
	    }
	    
}
